import { ADMIN } from '../actionTypes';
import fetch from '../utils/fetch';
import config from '../config';
import { message } from 'antd';
import * as R from 'ramda';

export const update = (payload) => ({
  type: ADMIN,
  payload,
});

export const getAll = () => async (dispatch, getState) => {

  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/carousel/all`,
    headers: {
      // authorization: token,
    }
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    const carousel = R.map((item) => {
      item.key = item.id;
      return item;
    })(res.data);
    dispatch(update({ carousel, }));
  } else {
    message.error('网络服务异常');
  }
};

export const changeRank = (val, { id }) => (dispatch, getState) => {
  const { carousel } = getState().admin;
  const index = R.findIndex(R.propEq('id', id))(carousel);
  const arr = R.adjust(item => {
    item.rank = +val;
    return item;
  }, index)(carousel);
  dispatch(update({ carousel: arr }));
};

/**
 * 新增轮播图
 */
export const add = () => (dispatch, getState) => {
  const { carousel } = getState().admin;
  const ids = R.compose(
    R.sort((a, b) => a - b),
    R.pluck('id')
  )(carousel);
  const curId = ids[ids.length - 1] + 1;
  carousel.push({
    key: curId,
    id: curId,
    rank: carousel.length,
    url: '',
  });
  dispatch(update({ carousel, }));
};

/**
 * 删除轮播图
 */
export const del = (id) => async (dispatch, getState) => {
  const { carousel } = getState().admin;
  if (carousel.length === 1) {
    message.error('至少需要一个轮播图');
    return false;
  }

  const res = await fetch({
    method: 'POST',
    url: `${config.host}/api/carousel/del`,
    param: {
      id,
    }
  }).catch(() => {
    message.error('网络异常');
  });

  if (res.code === 200) {
    message.success('删除成功');
    const arr = R.reject(item => item.id === +id)(carousel);
    dispatch(update({ carousel: arr }));
  } else {
    message.error('删除失败');
  }
};

/**
 * 上传图片后更新数据
 * @param id
 * @param url
 */
export const updateImg = ({ id, url }) => (dispatch, getState) => {
  const { carousel } = getState().admin;
  const index = R.findIndex(R.propEq('id', id))(carousel);
  const arr = R.adjust(item => {
    item.url = url;
    return item;
  }, index)(carousel);
  console.log(arr);
  dispatch(update({ carousel: arr }));
};

/**
 * 更新数据
 */
export const submit = () => async (dispatch, getState) => {
  const { carousel } = getState().admin;
  if (!Array.isArray(carousel) || carousel.length === 0) {
    message.error('至少需要一个图片');
    return false;
  }

  if (R.filter(item => item.url === '')(carousel).length > 0) {
    message.error('请上传图片后再更新');
    return false;
  }

  console.log(carousel);

  const res = await fetch({
    method: 'POST',
    url: `${config.host}/api/carousel/edit`,
    param: {
      carousel,
    }
  }).catch(() => {
    message.error('网络异常');
  });

  if (res.code === 200) {
    message.success('更新成功');
    setTimeout(() => {
      location.reload();
    }, 1200);
  } else {
    message.error('更新失败');
  }

};
